package com.dajingling.filter;

import com.dajingling.util.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter({"*.do","/admin/index.html","/admin/view/*"})
public class AccessControlFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String username = UserUtil.getUserName(request.getSession());
        String requestURI = request.getRequestURI();
        System.out.print("用户："+username+",正在访问："+requestURI);
        if (username != null || UserUtil.getWxUser(request.getSession())!=null || requestURI.endsWith("login.do") || requestURI.endsWith("/") || requestURI.endsWith("wxlogin.do") || requestURI.endsWith("wxloginSms.do") || requestURI.endsWith("/wxloginpwd.do")) {
            System.out.println(",已允许");
            filterChain.doFilter(servletRequest, servletResponse);
        }else {
            System.out.println(",权限不足");
            response.sendError(404,"很遗憾权限不足");
        }

    }

    @Override
    public void destroy() {

    }
}
